﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Admin_AddStudent : System.Web.UI.Page
{
    Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["TYPE"] == null || Session["TYPE"].ToString() == "CUS")
        {
            Response.Write("<script>alert('登录信息错误！')</script>");
            Response.Write("<script>window.location.href='../index.aspx'</script>");
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlDataReader read = dataconn.ExceRead("select * from [USER] where TYPE='CUS' and LOGIN_NAME='" + this.txtLOGIN_NAME.Text + "'");
        read.Read();
        if (!CheckText())
            return;
        if (read.HasRows)
        {
            if (this.txtLOGIN_NAME.Text == read["LOGIN_NAME"].ToString())
            {
                Response.Write("<script>alert('该登录用户已存在！')</script>");
                Response.Write("<script>window.location.href='AddStudent.aspx'</script>");

            }
        }
        else
        {

            string ID = Guid.NewGuid().ToString();
            string sql = string.Format(@"insert into [USER](ID,TYPE,LOGIN_NAME,[USER_NAME],LOGIN_PASS,COMPANY,DEPARTMENT) values " +
                "('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", ID, "CUS", txtLOGIN_NAME.Text, txtUSER_NAME.Text, txtStuPwd.Text, txtCompany.Text, txtDepartment.Text);
            dataconn.eccom(sql);
            Response.Write("<script>alert('添加成功！')</script>");
            Response.Write("<script>window.location.href='AddStudent.aspx'</script>");
        }
        read.Close();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script>window.location.href='zhuce.aspx'</script>");
    }

    public bool CheckText()
    {
        if (txtCompany.Text == "" || txtDepartment.Text == "" || txtLOGIN_NAME.Text == "" || txtStuPwd.Text == "" || txtStuPwdR.Text == "" || txtUSER_NAME.Text == "")
        {
            Response.Write("<script>alert('请完善表格！')</script>");
            return false;
        }
        else
            return true;
    }
}